package com.feng.online_xdclass.service.Impl;


import com.feng.online_xdclass.config.CacheKeyManger;
import com.feng.online_xdclass.mapper.VideoMapper;

import com.feng.online_xdclass.model.entity.Video;
import com.feng.online_xdclass.model.entity.VideoBanner;
import com.feng.online_xdclass.service.VideoService;
import com.feng.online_xdclass.utils.BaseCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.ExecutionException;

@Service
@Transactional
public class VideoServiceImpl implements VideoService {
    @Autowired
    private VideoMapper videoMapper;
    @Autowired
    private BaseCache baseCache;
    @Override
    public List<Video> videoList() {
        try {
            Object cacheObj=baseCache.getTenMinuteCache().get(CacheKeyManger.INDEX_VIDEL_LIST,()->{
               List<Video> videoList= videoMapper.videoList();
               return videoList;
            });
            if(cacheObj instanceof List){
                List<Video> videoList=(List<Video>)cacheObj;
                return videoList;
            }
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<VideoBanner> listBanner() {
        try {
            Object cacheobj=baseCache.getTenMinuteCache().get(CacheKeyManger.INDEX_BANNER_KEY,()->{
                List<VideoBanner> bannerList=videoMapper.listVideoBanner();
                System.out.println("从数据库里面找轮播图列表");
                return bannerList;
            });
            if(cacheobj instanceof List){
                List<VideoBanner> bannerList= (List<VideoBanner>)cacheobj;
                return bannerList;
            }
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Video findDetailById(int videoId) {
        //TODO 需要使用Mybatis关联复杂查询
        String videoCacheKey=String.format(CacheKeyManger.VIDEO_DETAIL,videoId);
        try {
            Object cacheObject=baseCache.getOneHourCache().get(videoCacheKey,()->{
                Video video=videoMapper.findDetailById(videoId);
                return video;
            });
            if(cacheObject instanceof Video){
                Video video=(Video)cacheObject;
                return video;
            }
        } catch (ExecutionException e) {
            e.printStackTrace();
        }


        return null;
    }
}
